package com.yunxi.core.dao;

import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.yunxi.core.web.LoginInfo;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Date;

@Component
public class MetaHandler implements MetaObjectHandler {

    private static final Logger logger = LoggerFactory.getLogger(MetaHandler.class);

    /**
     * 插入操作：自动填充
     */
    public void insertFill(MetaObject metaObject) {
        // 获取到需要被填充的字段值
        // 创建时间
        setFieldValByName("createTime", new Date(), metaObject);
        setFieldValByName("createDate", new Date(), metaObject);
        setFieldValByName("updateDate", new Date(), metaObject);
        setFieldValByName("updateTime", new Date(), metaObject);
        //版本号
//        setFieldValByName("version", 0, metaObject);
        setFieldValByName("isSend", 0, metaObject);
        setFieldValByName("isDel", 0, metaObject);
        String currentUserStr = RpcContext.getContext().getAttachment("currentUser");
        if (currentUserStr != null) {//存在用户信息
            try {
                LoginInfo loginInfo = JSONObject.parseObject(currentUserStr, LoginInfo.class);
                if (loginInfo != null) {
                    Class<?> aClass = metaObject.getOriginalObject().getClass();
                    Field[] fields = aClass.getDeclaredFields();
                    for (int i = 0; i < fields.length; i++) {
                        Field field = fields[i];
                        // System.out.println(field.getName());
                        if (field.getName().equals("createBy")) {
                            String type = field.getGenericType().toString();
                            if ("class java.lang.String".equals(type)) {
                                setFieldValByName("createBy", loginInfo.getUserCode(), metaObject);
                            } else {
                                setFieldValByName("createBy", loginInfo.getUserCode(), metaObject);
                            }
                        }
                    }
                }

            } catch (Exception e) {
                logger.debug(e.toString());
            }
        }

//        //创建人
//        if (RequestContextHolder.getRequestAttributes() != null) {
//            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
//                .getRequest();
//            LoginInfo u = (LoginInfo) request.getAttribute("currentUser");
//            if (u != null) {
//                Class<?> aClass = metaObject.getOriginalObject().getClass();
//                Field[] fields = aClass.getDeclaredFields();
//                for (int i = 0; i < fields.length; i++) {
//                    Field field = fields[i];
//                    // System.out.println(field.getName());
//                    if (field.getName().equals("createBy")) {
//                        String type = field.getGenericType().toString();
//                        if ("class java.lang.String".equals(type)) {
////                            setFieldValByName("createBy", u.getUserId(), metaObject);
//                        } else {
////                            setFieldValByName("createBy", u.getUserId(), metaObject);
//                        }
//                    }
//                }
//            }
//        }
    }

    /**
     * 更新操作：自动填充
     */
    public void updateFill(MetaObject metaObject) {
        // 获取到需要被填充的字段值
        setFieldValByName("updateDate", new Date(), metaObject);
        setFieldValByName("updateTime", new Date(), metaObject);
        //修改人
        String currentUserStr = RpcContext.getContext().getAttachment("currentUser");
        if (currentUserStr != null) {//存在用户信息
            try {
                LoginInfo loginInfo = JSONObject.parseObject(currentUserStr, LoginInfo.class);
                if (loginInfo != null) {
                    setFieldValByName("updateBy", loginInfo.getUserCode(),metaObject);
//                    Class<?> aClass = metaObject.getOriginalObject().getClass();
//                    Field[] fields = aClass.getDeclaredFields();
//                    for (int i = 0; i < fields.length; i++) {
//                        Field field = fields[i];
//                        if (field.getName().equals("updateBy")) {
//                            String type = field.getGenericType().toString();
//                            if ("class java.lang.String".equals(type)) {
//                                setFieldValByName("updateBy", loginInfo.getUserId().toString(), metaObject);
//                            } else {
//                                setFieldValByName("updateBy", loginInfo.getUserId(), metaObject);
//                            }
//                        }
//                    }
                }

            } catch (Exception e) {
                logger.debug(e.toString());
            }
        }

//        if (RequestContextHolder.getRequestAttributes() != null) {
//            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
//                .getRequest();
//            LoginInfo u = (LoginInfo) request.getAttribute("currentUser");
//            if (u != null) {
//                Class<?> aClass = metaObject.getOriginalObject().getClass();
//                Field[] fields = aClass.getDeclaredFields();
//                for (int i = 0; i < fields.length; i++) {
//                    Field field = fields[i];
//                    if (field.getName().equals("updateBy")) {
//                        String type = field.getGenericType().toString();
//                        if ("class java.lang.String".equals(type)) {
////                            setFieldValByName("updateBy", u.getUserId().toString(), metaObject);
//                        } else {
////                            setFieldValByName("updateBy", u.getUserId(), metaObject);
//                        }
//                    }
//                }
//            }
//        }
        //setFieldValByName("updateBy", "admin", metaObject);
    }
}